<?php
namespace app\admin\controller;

use app\admin\model\AdminGroup as AdminGroupModel;
use app\admin\model\Menu as MenuModel;
//use app\admin\model\AdminJob as AdminJobModel;
use think\Db;
use think\Request;

class AdminGroup extends Base
{
    public function index()
    {
        $model = new AdminGroupModel;
        $menuModel = new MenuModel;

        //渲染页面模板
        $model->field('g.id,g.name as gname,g.flag,g.created_at,g.updated_at,g.power, g.illustration as gillustration,g.menu_id')
            ->alias('g')
            //->join('menu m','g.menu_id = m.id','left')
           // ->join('admin_job j','g.admin_job_id = j.id','left')
            ->where('g.flag','0')
            ->order('g.id','desc');
        //  ->select();
        //  ->paginate(5);

        //搜索名称
        $phname = $this->request->param("phname");

        if(!empty($phname)){
            $model->where('g.name','like', '%'.$phname.'%');
        }
        $result = $model->paginate(10,false,[
            'query'=>['phname'=>$phname]
        ]);

        $menu = $menuModel->field('id,name')->where('flag','1')->where('id','not in','18,31,26,27')->select();//,19,13,14,

        //计算总共记录条数
        $count = $result->total();
        //分页
        $page = $result->render();

        //接收ajax传递的值
        $request = request();
        $ispost = $request->isPost();
        if($ispost){
            $id = $_POST["viewId"];

            $view = $model->getAll()->where("id",$id)->find();
            $view['menu_id']=explode(",",$view['menu_id']);

            return json([
                'code' => 200,
                'view'=>$view,
            ]);
        }

        $this->assign('currentPage',$result->currentPage());
        $this->assign('lastPage',$result->lastPage());
        $this->assign('page',$page);
        $this->assign('menu',$menu);
        $this->assign('admingroup',$result);
        $this->assign('count',$count);
        $this->assign('empty',"暂无数据");
        return $this->fetch();
    }

    //新增数据
    public function create(){
        $model = new AdminGroupModel;
        //接收ajax传递的值
        $ispost = $this->request->isPost();

        if($ispost){
            $formdata = $this->request->post();
            $username = Db::query("select `id`,`name` from ". $this->dbTablePrefix ."admin_group where `name`='".$formdata['name']."' and deleted_at is NULL ");
            if(!empty($username)){
                return json([
                    'code' => 4000,
                    'message'=>"用户名已存在"
                ]);
            }
            $formdata['menu_id'] = implode(',',$formdata['menu_id']);
            $result = $model->allowField(true)->save($formdata);

            if($result == 1){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }

    //修改数据处理
    public function edit($id)
    {
        $model = new AdminGroupModel;
        //接收ajax传递的值
        $ispost = $this->request->isPost();
        if($ispost){
            $formdata = $this->request->post();
            $formdata['menu_id'] = implode(',',$formdata['menu_id']);

            $result = $model->allowField(true)->save($formdata,['id'=>$id]);

            if($result == 1){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }

    //删除单条记录
    public function delete($id)
    {
        $result = AdminGroupModel::update(['deleted_at'=>date('Y-m-d H:i:s')],['id'=>$id]);//软删除

        if($result){
            return json([
                'code' => 200,
            ]);
        }

    }

    //批量删除
    public function deleteArr()
    {
        $ispost = $this->request->isPost();
        if ($ispost) {
            $idArr = $this->request->post();

            $result = AdminGroupModel::where('id','exp','in('.implode(',',$idArr['id']).')')->update(['deleted_at'=>date('Y-m-d H:i:s')]);//软删除

            if($result){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }

    //新增权限
    public function createJob(){
        $model = new AdminJobModel;
        //接收ajax传递的值
        $ispost = $this->request->isPost();
        if($ispost){
            $formdata = $this->request->post();
            $name = Db::query("select id,name from ". $this->dbTablePrefix ."admin_job where `name`='".$formdata['name']."' and deleted_at is NULL ");
            if(!empty($name)){
                return json([
                    'code' => 4000,
                    'message'=>"权限已存在"
                ]);
            }
            $result = $model->allowField(true)->save($formdata);
            if($result == 1){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }
}